package com.cpimex.eprice.action.plugin;

import java.sql.SQLException;

import javax.naming.Context;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.sql.DataSource;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;


public class AppInit implements PlugIn {

	private final String PATH = "path";
	
	public void destroy() {

	}


	protected String getContextPath(ActionServlet servlet) {
		//	Get the context path without the request.
		String contextPath = "";
		try {
			String path = servlet.getServletContext().getResource("/").getPath();
			contextPath = path.substring(1, path.lastIndexOf("/"));
			String[] str_ = contextPath.split("/", 2);
			contextPath = "/"+str_[1];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return contextPath;
	}


	public void init(ActionServlet servlet, ModuleConfig config)throws ServletException {
		ServletContext sc = servlet.getServletContext();
		sc.setAttribute(PATH,  "/EPRICE2/");
		try {
			segmento(servlet);
			logistica(servlet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * Listado de logisticas
	 * 
	 * @param servlet
	 */
	private void logistica(ActionServlet servlet)throws Exception{
		
		javax.naming.InitialContext context = null;
		java.sql.Connection conn = null;

		try{
			
			context = new javax.naming.InitialContext();
			//conn = com.cpimex.eprice.action.plugin.ConnectionBuilder.epriceConnection(context);
			//Connection conn = ConnectionBuilder.epriceConnection(context);
			//DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
			DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
			conn = dataSrc.getConnection();
			
			
			java.util.ArrayList arrL_logistica = com.cpimex.eprice.model.Logistica.listado(conn.createStatement());
			servlet.getServletContext().setAttribute("arrL_logistica", arrL_logistica);
			
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
	}finally{
		try{
			conn.close();
		}catch(Exception exception){
			
		}
		}
	}
	
	
	
	
	/**
	 * 
	 * 
	 * @param servlet
	 * @throws SQLException 
	 */
	private void segmento(ActionServlet servlet) throws SQLException{
		
		javax.naming.InitialContext context = null;
		//	DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
		java.sql.Connection conn = null;

		try{
			
			context = new javax.naming.InitialContext();
			//conn = com.cpimex.eprice.action.plugin.ConnectionBuilder.epriceConnection(context);
			//DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
			//Context envCtx = (Context) context.lookup("java:comp/env");
			DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");

			conn = dataSrc.getConnection();
			
			java.util.ArrayList arrL_segmento = com.cpimex.eprice.model.Segmento.segmento(conn);
			
			servlet.getServletContext().setAttribute("arrL_segmento", arrL_segmento);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				conn.close();
			}catch(Exception exception){
				
			}
		}
	}	
}
